package com.jeomo.mem.starter.service.impl;

import com.jeomo.base.starter.service.impl.BaseServiceImpl;
import com.jeomo.common.core.constant.SearchParam;
import com.jeomo.common.core.constant.Sort;
import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.masterdata.starter.service.ILevelService;
import com.jeomo.mem.starter.entity.MemberPointsDetail;
import com.jeomo.mem.starter.mapper.MemPointsDetailMapper;
import com.jeomo.mem.starter.mapper.MemberCardMapper;
import com.jeomo.mem.starter.service.IMemPointsDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员积分明细服务
 *
 * @author:Jackpot
 * @create 2020-10-30 15:42
 * @description:
 */
@Service
@Transactional
public class MemPointsDetailServiceImpl extends BaseServiceImpl<MemPointsDetailMapper, MemberPointsDetail>
    implements IMemPointsDetailService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private final String CARD_NO = "card_no";
    private final String MEMBER_PHONE = "member_phone";
    private final String POINTS_SRC = "src_type_code";
    private final String HANDLE_TIME = "handle_time";
    private final String TIME_FROM = "time_from";
    private final String TIME_TO = "time_to";

    @Resource
    private MemPointsDetailMapper memPointsDetailMapper;
    @Resource
    private MemberCardMapper memberCardMapper;
    @Resource
    private ILevelService levelService;

    @Override
    public PageResponseDto pageSearch(PageQueryDto dto) {

        Map<String, Object> searchParams = dto.getSearchParams();
        if(searchParams!=null){
            Map<String, Object> params = new HashMap<>(8);
            params.put(SearchParam.SEARCH_EQ+CARD_NO,searchParams.get(CARD_NO));
            params.put(SearchParam.SEARCH_EQ+MEMBER_PHONE,searchParams.get(MEMBER_PHONE));
            params.put(SearchParam.SEARCH_EQ+POINTS_SRC,searchParams.get(POINTS_SRC));
            params.put(SearchParam.SEARCH_GE+HANDLE_TIME,searchParams.get(TIME_FROM));
            params.put(SearchParam.SEARCH_LE+HANDLE_TIME,searchParams.get(TIME_TO));
            dto.setSearchParams(params);
        }
        Map<String, String> sort = new HashMap<>(8);
        sort.put(HANDLE_TIME, Sort.DESC);
        dto.setSorts(sort);
        PageResponseDto pageResponseDto = super.pageSearch(dto);
        Map<String, String> levelMap = levelService.queryCache();
        List<?> items = pageResponseDto.getItems();
        for (Object item : items) {
            MemberPointsDetail detail = (MemberPointsDetail)item;
            detail.setLevelName(levelMap.get(detail.getLevelCode()));
        }
        return pageResponseDto;
    }

}
